package com.easy.product.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SeckillProduct;
import com.easy.common.service.SeckillProductService;
import com.easy.product.mapper.SeckillProductMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SeckillProductServiceImpl extends ServiceImpl<SeckillProductMapper, SeckillProduct> implements SeckillProductService {
    @Override
    public SeckillProduct getProductById(Integer id) {
        return super.getById(id);
    }

    @Override
    public SeckillProduct getProductByActivityId(Integer activityId) {
        LambdaQueryWrapper<SeckillProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillProduct::getActivityId, activityId);
        queryWrapper.eq(SeckillProduct::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public SeckillProduct getProductByProductId(Integer productId) {
        LambdaQueryWrapper<SeckillProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillProduct::getProductId, productId);
        queryWrapper.eq(SeckillProduct::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public SeckillProduct getProductByStatus(Integer status) {
        LambdaQueryWrapper<SeckillProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillProduct::getStatus, status);
        queryWrapper.eq(SeckillProduct::getIsdel, 0);
        return super.getOne(queryWrapper);
    }

    @Override
    public int addProduct(SeckillProduct seckillProduct) {
        return super.save(seckillProduct)?1:0;
    }

    @Override
    public int updateProduct(SeckillProduct seckillProduct) {
        return super.updateById(seckillProduct)?1:0;
    }

    @Override
    public int deleteProduct(Integer id) {
        return super.removeById(id)?1:0;
    }

    @Override
    public int reverseStatus(Integer id) {
        SeckillProduct product = super.getById(id);
        if (product != null) {
            product.setStatus(product.getStatus() == 1 ? 0 : 1);
        }
        if (super.updateById(product)) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<SeckillProduct> getAll(SeckillProduct seckillProduct) {
        LambdaQueryWrapper<SeckillProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SeckillProduct::getId, seckillProduct.getId());
        return super.list(queryWrapper);
    }

    @Override
    public IPage<SeckillProduct> getPage(SeckillProduct seckillProduct, IPage page) {
        LambdaQueryWrapper<SeckillProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SeckillProduct::getId, seckillProduct.getId());
        return super.page(page);
    }
}
